home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 4 / adb / s-direio < prev    next >
Text File  |  1996-02-12  |  11KB  |  351 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                     S Y S T E M . D I R E C T _ I O                      --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.9 $                              --
  10. --                                                                          --
  11. --        Copyright (C) 1992,1993,1994 Free Software Foundation, Inc.       --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with Ada.IO_Exceptions;         use Ada.IO_Exceptions;
  37. with Interfaces.C_Streams;      use Interfaces.C_Streams;
  38. with System;                    use System;
  39. with System.File_IO;
  40. with System.Tasking_Soft_Links;
  41. with Unchecked_Deallocation;
  42.  
  43. package body System.Direct_IO is
  44.  
  45.    package FIO renames System.File_IO;
  46.    subtype AP is FCB.AFCB_Ptr;
  47.    use type FCB.Shared_Status_Type;
  48.  
  49.    -----------------------
  50.    -- Local Subprograms --
  51.    -----------------------
  52.  
  53.    procedure Set_Position (File : in File_Type);
  54.    --  Sets file position pointer according to value of current index
  55.  
  56.    -------------------
  57.    -- AFCB_Allocate --
  58.    -------------------
  59.  
  60.    function AFCB_Allocate (Control_Block : Direct_AFCB) return FCB.AFCB_Ptr is
  61.    begin
  62.       return new Direct_AFCB;
  63.    end AFCB_Allocate;
  64.  
  65.    ----------------
  66.    -- AFCB_Close --
  67.    ----------------
  68.  
  69.    --  No special processing required for Direct_IO close
  70.  
  71.    procedure AFCB_Close (File : access Direct_AFCB) is
  72.    begin
  73.       null;
  74.    end AFCB_Close;
  75.  
  76.    ---------------
  77.    -- AFCB_Free --
  78.    ---------------
  79.  
  80.    procedure AFCB_Free (File : access Direct_AFCB) is
  81.  
  82.       type FCB_Ptr is access all Direct_AFCB;
  83.  
  84.       FT : FCB_Ptr := File;
  85.  
  86.       procedure Free is new
  87.         Unchecked_Deallocation (Direct_AFCB, FCB_Ptr);
  88.  
  89.    begin
  90.       Free (FT);
  91.    end AFCB_Free;
  92.  
  93.    ------------
  94.    -- Create --
  95.    ------------
  96.  
  97.    procedure Create
  98.      (File : in out File_Type;
  99.       Mode : in FCB.File_Mode := FCB.Inout_File;
  100.       Name : in String := "";
  101.       Form : in String := "")
  102.    is
  103.       File_Control_Block : Direct_AFCB;
  104.  
  105.    begin
  106.       FIO.Open (File_Ptr  => AP (File),
  107.                 Dummy_FCB => File_Control_Block,
  108.                 Mode      => Mode,
  109.                 Name      => Name,
  110.                 Form      => Form,
  111.                 Amethod   => 'D',
  112.                 Creat     => True,
  113.                 Text      => False);
  114.    end Create;
  115.  
  116.    -----------------
  117.    -- End_Of_File --
  118.    -----------------
  119.  
  120.    function End_Of_File (File : in File_Type) return Boolean is
  121.    begin
  122.       FIO.Check_Read_Status (AP (File));
  123.       return Count (File.Index) > Size (File);
  124.    end End_Of_File;
  125.  
  126.    -----------
  127.    -- Index --
  128.    -----------
  129.  
  130.    function Index (File : in File_Type) return Positive_Count is
  131.    begin
  132.       FIO.Check_File_Open (AP (File));
  133.       return Count (File.Index);
  134.    end Index;
  135.  
  136.    ----------
  137.    -- Open --
  138.    ----------
  139.  
  140.    procedure Open
  141.      (File : in out File_Type;
  142.       Mode : in FCB.File_Mode;
  143.       Name : in String;
  144.       Form : in String := "")
  145.    is
  146.       File_Control_Block : Direct_AFCB;
  147.  
  148.    begin
  149.       FIO.Open (File_Ptr  => AP (File),
  150.                 Dummy_FCB => File_Control_Block,
  151.                 Mode      => Mode,
  152.                 Name      => Name,
  153.                 Form      => Form,
  154.                 Amethod   => 'D',
  155.                 Creat     => False,
  156.                 Text      => False);
  157.    end Open;
  158.  
  159.    ----------
  160.    -- Read --
  161.    ----------
  162.  
  163.    procedure Read
  164.      (File : in File_Type;
  165.       Item : Address;
  166.       Size : in Interfaces.C_Streams.size_t;
  167.       From : in Positive_Count)
  168.    is
  169.    begin
  170.       Set_Index (File, From);
  171.       Read (File, Item, Size);
  172.    end Read;
  173.  
  174.    procedure Read
  175.      (File : in File_Type;
  176.       Item : Address;
  177.       Size : in Interfaces.C_Streams.size_t)
  178.    is
  179.    begin
  180.       FIO.Check_Read_Status (AP (File));
  181.  
  182.       --  If last operation was not a read, or if in file sharing mode,
  183.       --  then reset the physical pointer of the file to match the index
  184.       --  We lock out task access over the two operations in this case.
  185.  
  186.       if File.Last_Op /= Op_Read
  187.         or else File.Shared_Status = FCB.Yes
  188.       then
  189.          if End_Of_File (File) then
  190.             raise End_Error;
  191.          end if;
  192.  
  193.          System.Tasking_Soft_Links.Lock_Task;
  194.          Set_Position (File);
  195.          FIO.Read_Buf (AP (File), Item, Size);
  196.          System.Tasking_Soft_Links.Unlock_Task;
  197.  
  198.       else
  199.          FIO.Read_Buf (AP (File), Item, Size);
  200.       end if;
  201.  
  202.       File.Index := File.Index + 1;
  203.  
  204.       --  Set last operation to read, unless we did not read a full record
  205.       --  (happens with the variant record case) in which case we set the
  206.       --  last operation as other, to force the file position to be reset
  207.       --  on the next read.
  208.  
  209.       if File.Bytes = Size then
  210.          File.Last_Op := Op_Read;
  211.       else
  212.          File.Last_Op := Op_Other;
  213.       end if;
  214.    end Read;
  215.  
  216.    --  The following is the required overriding for Stream.Read, which is
  217.    --  not used, since we do not do Stream operations on Direct_IO files.
  218.  
  219.    procedure Read
  220.      (File : in out Direct_AFCB;
  221.       Item : out Ada.Streams.Stream_Element_Array;
  222.       Last : out Ada.Streams.Stream_Element_Offset)
  223.    is
  224.    begin
  225.       raise Program_Error;
  226.    end Read;
  227.  
  228.    -----------
  229.    -- Reset --
  230.    -----------
  231.  
  232.    procedure Reset (File : in out File_Type; Mode : in FCB.File_Mode) is
  233.    begin
  234.       FIO.Reset (AP (File), Mode);
  235.       File.Index := 1;
  236.       File.Last_Op := Op_Read;
  237.    end Reset;
  238.  
  239.    procedure Reset (File : in out File_Type) is
  240.    begin
  241.       FIO.Reset (AP (File));
  242.       File.Index := 1;
  243.       File.Last_Op := Op_Read;
  244.    end Reset;
  245.  
  246.    ---------------
  247.    -- Set_Index --
  248.    ---------------
  249.  
  250.    procedure Set_Index (File : in File_Type; To : in Positive_Count) is
  251.    begin
  252.       FIO.Check_File_Open (AP (File));
  253.       File.Index := Count (To);
  254.       File.Last_Op := Op_Other;
  255.    end Set_Index;
  256.  
  257.    ------------------
  258.    -- Set_Position --
  259.    ------------------
  260.  
  261.    procedure Set_Position (File : in File_Type) is
  262.    begin
  263.       if fseek
  264.            (File.Stream, long (File.Bytes) *
  265.               long (File.Index - 1), SEEK_SET) /= 0
  266.       then
  267.          raise Use_Error;
  268.       end if;
  269.    end Set_Position;
  270.  
  271.    ----------
  272.    -- Size --
  273.    ----------
  274.  
  275.    function Size (File : in File_Type) return Count is
  276.    begin
  277.       FIO.Check_File_Open (AP (File));
  278.       File.Last_Op := Op_Other;
  279.  
  280.       if fseek (File.Stream, 0, SEEK_END) /= 0 then
  281.          raise Device_Error;
  282.       end if;
  283.  
  284.       return Positive_Count (ftell (File.Stream) / long (File.Bytes));
  285.    end Size;
  286.  
  287.    -----------
  288.    -- Write --
  289.    -----------
  290.  
  291.    procedure Write
  292.      (File : File_Type;
  293.       Item : Address;
  294.       Size : in Interfaces.C_Streams.size_t;
  295.       To   : Positive_Count)
  296.    is
  297.    begin
  298.       Set_Index (File, To);
  299.       Write (File, Item, Size);
  300.    end Write;
  301.  
  302.    procedure Write
  303.      (File : File_Type;
  304.       Item : Address;
  305.       Size : in Interfaces.C_Streams.size_t)
  306.    is
  307.    begin
  308.       FIO.Check_Write_Status (AP (File));
  309.  
  310.       --  If last operation was not a write, or if in file sharing mode,
  311.       --  then reset the physical pointer of the file to match the index
  312.       --  We lock out task access over the two operations in this case.
  313.  
  314.       if File.Last_Op /= Op_Write
  315.         or else File.Shared_Status = FCB.Yes
  316.       then
  317.          System.Tasking_Soft_Links.Lock_Task;
  318.          Set_Position (File);
  319.          FIO.Write_Buf (AP (File), Item, Size);
  320.          System.Tasking_Soft_Links.Unlock_Task;
  321.       else
  322.          FIO.Write_Buf (AP (File), Item, Size);
  323.       end if;
  324.  
  325.       File.Index := File.Index + 1;
  326.  
  327.       --  Set last operation to write, unless we did not read a full record
  328.       --  (happens with the variant record case) in which case we set the
  329.       --  last operation as other, to force the file position to be reset
  330.       --  on the next write.
  331.  
  332.       if File.Bytes = Size then
  333.          File.Last_Op := Op_Write;
  334.       else
  335.          File.Last_Op := Op_Other;
  336.       end if;
  337.    end Write;
  338.  
  339.    --  The following is the required overriding for Stream.Write, which is
  340.    --  not used, since we do not do Stream operations on Direct_IO files.
  341.  
  342.    procedure Write
  343.      (File : in out Direct_AFCB;
  344.       Item : in Ada.Streams.Stream_Element_Array)
  345.    is
  346.    begin
  347.       raise Program_Error;
  348.    end Write;
  349.  
  350. end System.Direct_IO;
  351.